package com.goldenautumn.prefabricate.activity.category

import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.LogUtils
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.activity.account.LoginActivity
import com.goldenautumn.prefabricate.activity.home.HomeMenuAdapter
import com.goldenautumn.prefabricate.activity.home.TypeFragment
import com.goldenautumn.prefabricate.activity.mall.adapter.CenterLayoutManager
import com.goldenautumn.prefabricate.activity.search.SearchActivity
import com.goldenautumn.prefabricate.business.mall.HomeDataUiState
import com.goldenautumn.prefabricate.business.mall.MallViewModel
import com.goldenautumn.prefabricate.business.user.LoadingUiState
import com.goldenautumn.prefabricate.databinding.FragmentCategoryBinding
import com.gyf.immersionbar.ImmersionBar
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select
import qncx.external.common.base.BaseFragment
import qncx.external.common.compat.visible
import qncx.external.common.utils.intent
import qncx.external.common.widget.tabLayout.ViewPager2Adapter

class CategoryFragment : BaseFragment<FragmentCategoryBinding>() {

    private val mMallModel by viewModels<MallViewModel>()

    private val mTypeAdapter by lazy {
        GoodsCategoryAdapter()
    }

    private var selectType:String? = null

    var centerLayoutManager: CenterLayoutManager? = null

    override fun getLayoutId(): Int = R.layout.fragment_category

    override suspend fun initView() {
        immersionBar()
        ImmersionBar.setTitleBar(this, mBinding.toolbar)

        mBinding.searchBtn.setOnClickListener {
            startActivity(SearchActivity::class.intent)
        }

        centerLayoutManager = CenterLayoutManager(mContext,RecyclerView.HORIZONTAL,false)
        mBinding.categoryRv.layoutManager = centerLayoutManager
        mBinding.categoryRv.adapter = mTypeAdapter

        mTypeAdapter.setOnItemClickListener { _, _, position ->
            centerLayoutManager?.smoothScrollToPosition(mBinding.categoryRv, RecyclerView.State(), position)
            mTypeAdapter.selectPosition = position
            mTypeAdapter.notifyDataSetChanged()
            mBinding.viewPager.setCurrentItem(position, false)
        }

        mBinding.viewPager.isUserInputEnabled = false
        mMallModel.getCategoryList()

        while (isActive) {
            select {
                mMallModel.homeDataReq.onReceive {
                    when(it) {
                        is HomeDataUiState.CategoryGoods -> {
                            var fragment = ArrayList<Fragment>()
                            it.category.forEach { menu ->
                                fragment.add(TypeFragment.newInstance("${menu.category_id}",false))
                            }
                            mTypeAdapter.submitList(it.category)
                            mBinding.viewPager.adapter = ViewPager2Adapter(childFragmentManager,lifecycle,fragment)
                            if(selectType != null) {
                                onTypeSelect(selectType)
                                selectType = null
                            }
                        }
                        else -> {}
                    }
                }
            }
        }

    }

    fun onTypeSelect(type:String?) {
        var position = -1
        mTypeAdapter.items.forEachIndexed { index, homeItemDataVo ->
            if(homeItemDataVo.category_id == type) position = index
        }
        if(position != -1) {
            mTypeAdapter.selectPosition = position
            mTypeAdapter.notifyDataSetChanged()
            mBinding.viewPager.setCurrentItem(position, false)
        } else selectType = type
    }

    override fun onResume() {
        super.onResume()
        immersionBar()
    }

}